package com.gec.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.entity.SysBuild;
import com.gec.model.entity.SysHouse;
import com.gec.model.vo.SysBuildQueryVo;
import com.gec.model.vo.SysHouseQueryVo;
import com.gec.model.entity.SysHouse;
import com.gec.model.entity.SysRole;
import com.gec.system.service.SysHouseService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 房屋表控制器
 * </p>
 *
 * @author Liangxj
 * @since 2023-12-06
 */
@Api(tags = "房屋管理控制器")
@RestController
@RequestMapping("/admin/system/sysHouse")
public class SysHouseController {


    @Autowired
    private SysHouseService sysHouseService;

    // 分页和条件查询
    @ApiOperation("分页和条件查询")
    @GetMapping("/{page}/{limit}")
    public Result selectHousePageVo(@PathVariable Long page,
                                    @PathVariable Long limit,
                                    SysHouseQueryVo sysHouseQueryVo
    ){
        IPage<SysHouse> iPage =new Page<>(page,limit);

        System.out.println("iPage条件："+iPage);

        iPage=this.sysHouseService.selectPage(iPage,sysHouseQueryVo);

        System.out.println("控制层打印楼栋信息："+iPage);
        return Result.ok(iPage);
    }

    //添加房产
    @ApiOperation("添加房产")
    @PostMapping("/addHouse")
    public Result addHouse(@RequestBody SysHouse sysHouse){

        //从数据库获取最后一条房屋数据的code拆分出"fw"+编号，编号累加
        String  code= this.sysHouseService.getCode();
        sysHouse.setCode(code);
        boolean save = this.sysHouseService.save(sysHouse);
        return save ? Result.ok() : Result.fail();
    }
    //删除单个房屋
    @ApiOperation("删除房屋")
    @GetMapping("/removeHouseById/{id}")
    public Result removeHouseById(@PathVariable Long id){
        boolean b = this.sysHouseService.removeById(id);
        return b ? Result.ok():Result.fail();
    }

    //删除多个房屋
    @ApiOperation("删除多个房屋")
    @DeleteMapping("/removeHouseByIds")
    public Result removeHouseByIds(@RequestBody List<Long> ids){
        boolean b = this.sysHouseService.removeByIds(ids);
        return b ? Result.ok():Result.fail();
    }
//
    //根据id查询当前要修改的房屋
    @ApiOperation("根据id查询当前要修改的房屋")
    @GetMapping("/finHouseById/{id}")
    public Result finHouseById(@PathVariable Long id){
        SysHouse byId = this.sysHouseService.getById(id);
        return Result.ok(byId);
    }
    //修改
    @ApiOperation("根据id修改当前房屋")
    @PostMapping("/updateHouse")
    public Result updateHouse(@RequestBody SysHouse sysHouse){
        boolean b = this.sysHouseService.updateById(sysHouse);
        return b ? Result.ok():Result.fail();
    }

    //    显示所有
    @ApiOperation("查询所有房产")
    @GetMapping("/getHouseList")
    public Result findAll() {
        List<SysHouse> list = this.sysHouseService.list();
        return Result.ok(list);
    }


}

